<?php
namespace jy_mjicai;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商,$isset无条件成为分销商
	*/
	public function checkmember($mid=0,$isexi=false,$isset=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'代理商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
			}
			if(!$member['is_agent']){
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->agentsetting['become']==0 || $isset || $this->session('becomefromuser')){
					//分销商无需审核，此处自动审核
					$ispass=true;
					$this->apply_agent($member['mid']);
					if($this->agentsetting['become_check'] && $isexi){
						exi('申请正在审核请耐心等待！','error','jy_mjicai/agent.apply/');
					}elseif($isset){
						return true;
					}
				}elseif($this->agentsetting['become']==2){
					//消费次数
					$buynums=pdo_count('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id');
					if($buynums>=$this->agentsetting['num']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->agentsetting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_mjicai/agent.apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mjicai/agent.apply/');
					}
				}elseif($this->agentsetting['become']==3){
					//消费金额
					$buyfee=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
					if($buyfee>=$this->agentsetting['money']){
						$ispass=true;
						$this->apply_agent($member['mid']);
						if($this->agentsetting['become_check'] && $isexi){
							exi('申请正在审核请耐心等待。','error','jy_mjicai/agent.apply/');
						}
					}elseif($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mjicai/agent.apply/');
					}
				}elseif($this->agentsetting['become']==4){
					//购买指定商品
					$become_goods=$this->agentsetting['become_goods'];
					foreach($become_goods as &$bgv){
						$isbuy=pdo_count('jy_weishop_order_goods',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1),'id');
						if($isbuy>0){
							$ispass=true;
							$this->apply_agent($member['mid']);
							break;
						}
					}
					if($this->agentsetting['become_check'] && $isexi && $ispass){
						exi('您的申请，正在审核请耐心等待！','error','jy_mjicai/agent.apply/');
					}elseif($isexi && !$ispass){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mjicai/agent.apply/');
					}
				}else{
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mjicai/agent.apply/');
					}
				}
				if($ispass){
					exi('','','jy_mjicai/agent.index/');
				}
			}elseif($member['stop'] && $isexi){
				if($isset){
						return true;
				}
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_mjicai/agent.apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($isset){
					return true;
				}
				if($this->agentsetting['become_reg']==1){
					exi('请先完善信息','error','jy_mjicai/agent.info/');
				}else{
					
				}
			}
			return $member;
	}
	public function checklogin($storeid='',$area_agentid='',$area_code=''){
		global $_AR;
		if(!$_SESSION['uid']){
			$_SESSION['forward_url']=$_AR['access_json'];
			model('core/core')->checklogin();
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','fromuser','fromcode'));
			if(empty($member)){
				model('core/member')->logout();
				model('core/core')->checklogin('jy_mjicai/member.index/');
			}else{
				$user=pdo_get('jy_mjicai_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id'));
				if(!$user['id']){
					if($this->agentsetting['is_fromcode']){
						$fromuser=$member['fromcode'];
					}else{
						$fromuser=$member['fromuser']?:$_SESSION['fromuser'];
					}
					$insert=array(
						'mid'=>$_SESSION['uid'],
						'merchid'=>$_SESSION['onemerchid'],
						'uniacid'=>$_SESSION['uniacid'],
						'fromuser'=>$fromuser,
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_mjicai_member',$insert);
					if($insert['fromuser']){
						$this->model('member')->upyao_fee($insert['fromuser']);
					}
					//如果成为分销商无条件，此处自动申请成为分销商
					if($this->agentsetting['become']==0 || $this->session('becomefromuser')){
						//分销商无需审核，此处自动审核
						$this->apply_agent($_SESSION['uid'],true);
					}
				}
			}
		}
	}
	public function setfromuser($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$fromuser=pdo_getcolumn('jy_mjicai_agent_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if(!$fromuser && $_SESSION['fromuser'] && $_SESSION['fromuser'] !=$mid){
			if($this->agentsetting['is_fromcode']){
				$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','fromuser','fromcode'));
				$fromuser=$member['fromcode'];
			}else{
				$fromuser=$_SESSION['fromuser'];
			}
			pdo_update('jy_mjicai_member',array('fromuser'=>$fromuser),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}
		return false;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_mjicai_member',$update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		return true;
	}
	public function check_myshop($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member=model('core/member')->basic($mid);
		$getmember=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return false;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}		
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('分销商会员注册失败，缺少会员ID.');
		}
		$ismid=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		if(!$member['fromuser'] && $_SESSION['this']=='custom' && $this->agentsetting['become_child']==1){
			$member['fromuser']=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'fromuser');
			$member['fromuser']=$member['fromuser']?:$_SESSION['fromuser'];
		}
		$member['onemerchid']=$_SESSION['onemerchid'];
		$this->insert('member',$member);
		if($member['fromuser']){
			$this->set_parent($member['mid'],$member['fromuser']);
		}
		if(!$member['fromuser'] && $_SESSION['this']=='custom' && $this->agentsetting['become_child']==1){
			$fromuser=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'fromuser');
			if($fromuser){
				$this->set_parent($member['mid'],$fromuser);
			}
		}
		//如果成为分销商无条件，此处自动申请成为分销商
		if($this->agentsetting['become']==0 || $this->session('becomefromuser')){
			//分销商无需审核，此处自动审核
			$this->apply_agent($member['mid'],true);
		}
	}
	/*
		设置上下级关系
	*/
	public function set_parent($mid,$parent_id,$ischange=false){
		$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid),array('mid','is_agent','level_id','fromuser','createtime'));
		$old_fromuser=$member['fromuser'];
		$isuser=pdo_getcolumn('core_members',array('id'=>$parent_id),'id');
		//如果不存在该会员，已经是分销商或已经有上级了，取消执行。
		if(($old_fromuser || !$isuser || ($member['is_agent'] && $member['createtime']+60<TIMESTAMP)) && !$ischange){
			return true;
		}
		//如果他的上级是我
		$ta_fromuser=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$parent_id),'fromuser');
		if($ta_fromuser==$mid){
			return true;
		}
		//如果上级是自己，上级ID归零
		if($mid==$parent_id){
			return true;
		}
		if($this->agentsetting['is_fromcode'] && $mid==$_SESSION['uid']){
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','fromuser','fromcode'));
			$parent_id=$member['fromcode'];
		}
		$this->update('member',array('fromuser'=>$parent_id),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		if($this->agentsetting['level']>0){
			$fromuser=$old_fromuser;
			for($i=0;$i<$this->agentsetting['level'];$i++){
				if(!$fromuser){
					break;
				}
				$fromuser=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$fromuser),'fromuser');
			}
			$fromuser=$parent_id;
			for($i=0;$i<$this->agentsetting['level'];$i++){
				if(!$fromuser){
					break;
				}
				//只有一级才增加一级下级
				$member_update=array(
						'member_num +='=>1,
						'agent_num +='=>1,
				);
				$this->update('member',$member_update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$fromuser));
				$this->update_agent_level($fromuser);
				$fromuser=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$fromuser),'fromuser');
			}
		}
		return true;
	}
	/*
		申请成为分销商
	*/
	public function apply_agent($mid,$noapply=false){
		$member=$this->pdo_get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($member['sale_mid']){
			exi('已经是直销用户，不能成为分销商。','error');
		}
		$apply=$this->get('agent_apply',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		if($apply && $apply['status']!=2){
			if($apply['status']==1){
				$member_update=array(
					'is_agent'=>1,
					'agenttime'=>TIMESTAMP,
				);
				$this->update('member',$member_update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$apply['mid']));
			}
			return;
		}
		if($apply['status']!=2){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'mid'=>$mid,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('agent_apply',$new_apply);
			$apply_id=$this->insertid();
		}else{
			$this->update('agent_apply',array('status'=>0),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$apply_id=$apply['id'];
		}
		if(!$this->agentsetting['become_check']){
			//分销商无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		return true;
	}
	/*
		审核分销商
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('agent_apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'is_agent'=>1,
				'agenttime'=>TIMESTAMP,
			);
			$this->update('member',$member_update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$apply['mid']));
			//审核通过，此处增加上线的相关数据
			$fromuser=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$apply['mid']),'fromuser');
			for($i=0;$i<$this->agentsetting['level'];$i++){
				if(!$fromuser){
					break;
				}
				$agent_update=array('agent_num +='=>1);
				$this->update('member',$agent_update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$fromuser));
				$this->update_agent_level($fromuser);
				$fromuser=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$fromuser),'fromuser');
			}
		}else{
			$update['remark']=$remark;
		}
		$this->update('agent_apply',$update,array('id'=>$apply_id));
		return true;
	}
	/*
		禁用分销商
	*/
	public function stop_agent($fromuser){
		$this->update('member',array('stop'=>1),array('mid'=>$fromuser));
	}
	/*
		恢复分销商
	*/
	public function start_agent($fromuser){
		$this->update('member',array('stop'=>0),array('mid'=>$fromuser));
	}
	/*
		获取会员的各项参数
		
		问题：开启分销内购时
			推广订单如何算？
			下线如何算？
			
			计算下线时，分销级数在设置的级数基础上-1，其他不变
			计算推广订单时，在上面的基础上+自己的订单
	*/
	public function get_member($mid){
		$res=array();
		//获取一级下线数量
		$members=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid),array('mid','is_agent'),'mid');		
		if(!$members){
			$res=array(
				'member_num'=>'0',
				'agent_num'=>'0',
				'order_price'=>'0',
				'order_num'=>'0',
				'myorder_price'=>'0',
				'order_price_next'=>'0',
				'order_num_next'=>'0',
				'member_num_next'=>'0',
				'agent_num_next'=>'0',
			);
			return $res;
		}
		//获取直属下级分销商数量
		$res['agent_num_next']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'fromuser'=>$mid));
		$res['member_num_next']=count($members);
		$member_ids=array_keys($members);
		$res['order_num_next']=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids));
		$res['order_price_next']=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids),'order_price');
		//获取下线总数，向下遍历系统设置的指定分销层数
		$commission_level=intval($this->agentsetting['level']);
		if($this->agentsetting['self_commission']){
			//开启分销内购
			$commission_level=$commission_level-1;
		}
		if($commission_level>1){
			$next_ids=$member_ids;
			for($i=1;$i<$commission_level;$i++){
				$next_members=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$next_ids),array('mid','is_agent'),'mid');
				if(!$next_members){
					break;
				}
				$members=array_merge($next_members,$members);
				$next_ids=array_keys($next_members);
				$member_ids=array_merge($next_ids,$member_ids);
			}
		}
		$res['member_num']=count($member_ids);
		$res['order_num']=$this->count('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids));
		$res['order_price']=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$member_ids),'order_price');
		$res['myorder_price']=$this->sum('order',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid),'myorder_price');
		$res['agent_num']=0;
		foreach($members as $member){
			if($member['is_agent']){
				$res['agent_num']++;
			}
		}
		if($this->agentsetting['self_commission']){
			//开启分销内购，此处加上用户自己的订单作为推广订单的一部分
			$self_order_num=$this->count('order',array('mid'=>$mid));
			$self_order_money=$this->sum('order',array('mid'=>$mid),'order_price');
			$res['order_num_next']=$self_order_num;
			$res['order_price_next']=$self_order_money;
			$res['order_num']+=$self_order_num;
			$res['order_price']+=$self_order_money;
		}
		return $res;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$orderid=0){
		//
		$member=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		if(!$member['is_agent'] || $member['stop']){
			return;
		}
		$level=$this->get('agent_level',array('id'=>$member['level_id']));
		$parameter=array(
			'params'=>array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('agent_level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$downmids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		if($downmids){
			$downmids=array_keys($downmids);
		}
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		$is_level_up_one=$this->agentsetting['is_level_up_one'];
		$members=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$member['mid']),array('mid','fromuser'),'mid');
		if($members){
			$member_ids=array_keys($members);
			$member_ids[]=$member['mid'];
		}else{
			$member_ids=$member['mid'];
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=$is_level_up_one?false:true;
			foreach($level_up_types as $type){
				//留空默认满足
				if($type=='oneorder_price'){
					//单笔自购金额
					$member['oneorder_price']=pdo_get('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$member['mid']),'','price desc');
					if($member['oneorder_price']<$conditions['oneorder_price']['price']){
						$up=false;
						break;
					}
				}elseif($type=='myorder_price'){
					//自购金额
					$member['myorder_price']=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$member['mid']),'price');
					if($member['myorder_price']<$conditions['myorder_price']){
						$up=false;
						break;
					}
				}elseif($type=='order_price'){
					//团订单总金额（元）,团订单总数（个）
					$member['order_price']=pdo_getsum('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$member_ids),'price');
					if($member['order_price']<$conditions['order_price']){
						$up=false;
						break;
					}
				}elseif($type=='order_num'){
					//团订单总金额（元）,团订单总数（个）
					$member['order_num']=pdo_count('jy_weishop_order',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$member_ids));
					if($member['order_num']<$conditions['order_num']){
						$up=false;
						break;
					}
				}elseif($type=='member_num'){
					$member['member_num']=pdo_count('jy_mjicai_member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$member['mid']));
					if($member['member_num']<$conditions['member_num']){
						$up=false;
						break;
					}
				}elseif($type=='agent_num'){
					$member['agent_num']=pdo_count('jy_mjicai_member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$member['mid']));
					if($member['agent_num']<$conditions['agent_num']){
						$up=false;
						break;
					}
				}
				if(($type=='buy_goods' || $type=='downbuy_goods') && $lv['goods_ids']){
					$goods_ids=json_decode($lv['goods_ids'],true);
					foreach($goods_ids as $gv){
						    if($type=='buy_goods'){
								$isbuynum=pdo_getsum('jy_weishop_order_goods',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid'=>$mid,'goodsid'=>$gv['id']),'total');
								if($isbuynum>0){
									break;
								}
							}
							if($type=='downbuy_goods'){
								if($downmids && count($downmids)){
									$isbuynum=pdo_getnorepeat('jy_weishop_order_goods',array('merchid'=>$_SESSION['onemerchid'],'ispay'=>1,'mid' =>$downmids,'goodsid'=>$gv['id']),'mid');
									$isbuynum=count($isbuynum);
									if($isbuynum>=$conditions[$type]){
										break;
									}else{
										$isbuynum=0;
									}
								}
							}
					}
					if($is_level_up_one){
						if($isbuynum){
							$up=true;
							break;
						}
					}elseif(!$isbuynum){
						$up=false;
						break;
					}
				}elseif($type=='level_num'){
					//指定等级,指定等级人数（人）
					if($conditions['level_id']){
						$member['level_num']=$this->count('member',array('merchid'=>$_SESSION['onemerchid'],'level_id'=>$conditions['level_id'],'fromuser'=>$member['mid']));
					}
					if($is_level_up_one){
						if($member['level_num']>=$conditions['level_num'] || !$conditions['level_num']){
							$up=true;
							break;
						}
					}else{
						if($member['level_num']<$conditions['level_num'] && $conditions['level_num']){
							$up=false;
							break;
						}
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('member',$update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
		}
		return true;
	}
	public function min_agent_level($mid=0){
		//
		$mid=$mid?:$_SESSION['uid'];
		$member=$this->get('member',array('mid'=>$mid));
	}
	/*
		核实指定等级活动
	*/
	public function upgametime($mid_ids=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(count($mid_ids)){
			foreach($mid_ids as $mv){
				$level_id=$this->getcolumn('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mv),'level_id');
				$gamepost=array(
					'type'=>8,
					'level_id'=>$level_id
				);
				//model('jy_weishop/game')->up_addgame($gamepost,$mid);
			}
		}
	}
	/*
		邀请奖励
	*/
	public function upyao_fee($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if($this->agentsetting['yao_open']){
			$member=$this->get('member',array('mid'=>$mid));
			foreach($this->agentsetting['yao_set'] as $k=>$yv){
				if($yv['fee']>0 && $member['level_id']==$yv['level_id']){
					$isgetfee=$this->pdo_getsum('commission',array('type'=>4,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'money');
					if($isgetfee>=$yv['allfee'] && $yv['allfee']){
						continue;
					}
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'merchid'=>$_SESSION['onemerchid'],
						'mid'=>$mid,
						'type'=>4,
						'ordersn'=>'yao_set'.$k,
						'money'=>$yv['fee'],
						'createtime'=>TIMESTAMP,
						'status'=>2,
					);
					$this->insert('commission',$new_commission);
					$other_type=$this->agentsetting['other_type']?:1;
					if($_SESSION['onemerchid']){
						model('jy_merch/member')->member_credit($_SESSION['onemerchid'],$mid,'credit'.$other_type,$item['jifen'],true,'jy_mjicai','邀请奖励');
					}else{
						model('core/member')->member_credit($mid,'credit'.$other_type,$item['jifen'],true,'jy_mjicai','邀请奖励');
					}
				}
			}
		}
		return false;
	}
	public function recharge($mid=0,$fee=0,$type=0,$remark=''){
		$mid=$mid?:$_SESSION['uid'];
		if(!$fee || $fee<=0){
			return false;
		}
		$member=pdo_get('jy_mjicai_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$update=array(
			'baofee +='=>$fee,
		);
		$user=pdo_update('jy_mjicai_member',$update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$params=array(
			'fee'=>$fee,
			'mid'=>$mid,
		);
		$this->model('model')->add_feelog($params,7);
		return true;
	}
	public function uptaocan($order,$ismerchpay=0){
		$total=pdo_getcolumn('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id'],'mid'=>$order['mid']),'total');
		$total=$total?:1;
		$api_data=$order['api_data']?json_decode($order['api_data'],true):array();
		$taocan=$api_data['taocan'];
		if(!$taocan){
			return false;
		}
		$more['ismerchpay']=$ismerchpay;
		if($taocan['piao']>0){
			model('jy_merch/member')->member_credit($order['merchid'],$order['mid'],'credit4',$taocan['piao']*$total,true,'jy_mjicai','购买建仓商品，组合赠送','','',$more);
		}
		if($taocan['credit1']>0){
			model('jy_merch/member')->member_credit($order['merchid'],$order['mid'],'credit1',$taocan['credit1']*$total,true,'jy_mjicai','购买建仓商品，组合赠送','','',$more);
		}
		if($taocan['credit6']>0){
			model('jy_merch/member')->member_credit($order['merchid'],$order['mid'],'credit6',$taocan['credit6']*$total,true,'jy_mjicai','购买建仓商品，组合赠送','','',$more);
		}
		return true;
	}
	public function check_getmax($mid=0,$fee=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$this->setting['user_getmax'] || !$this->setting['open_getmax']){
			return false;
		}
		$member=pdo_get('jy_mjicai_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$isgetfee=pdo_getsum('jy_mjicai_feelog',array('type'=>0,'merchid'=>$_SESSION['onemerchid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fee');
		$maxfee=$this->setting['user_getmax']*$member['isdui_baofee'];
		if($maxfee<=$isgetfee){
			return true;
		}
		return false;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		if($post['fee']<=0){
			exi('没有填写申请金额！','error');
		}
		if(!$post['pay_type']){
			exi('请选择提现方式！','error');
		}
		if($_SESSION['onemerchid']){
			$member=model('jy_merch/member')->getdetail($_SESSION['onemerchid'],$_SESSION['uid']);
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit4','credit6'));
		}
		if($post['fee_type']!=4 && $post['fee_type']!=6){
			exi('提现类型不支持！'.$post['fee_type'],'error');
		}
		if($post['fee_type']==4 && $post['fee']>$member['credit4']){
			exi('账户余额不足不能提现！','error');
		}
		if($post['fee_type']==6 && $post['fee']>$member['credit4']){
			exi('账户余额不足不能提现！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'money'=>$post['fee'],
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$type=$post['fee_type']==4?19:20;
		if($_SESSION['onemerchid']){
			model('jy_merch/member')->member_credit($_SESSION['onemerchid'],$_SESSION['uid'],'credit'.$post['fee_type'],$new_balance['money'],false,'jy_mjicai','提现申请');
		}else{
			model('core/member')->member_credit($_SESSION['uid'],'credit'.$post['fee_type'],$new_balance['money'],false,'jy_mjicai','提现申请');
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$_SESSION['uid'],
			'fee'=>$new_balance['money'],
		);
		$this->model('model')->add_feelog($data,$type);
		return $balance_id;
	}
}